KEY Statements (Event Trapping) ---------------------------------------------------------------------------- Action Enable, disable, or suspend trapping of specified keys. Syntax KEY( n%) ON KEY( n%) OFF KEY( n%) STOP Remarks The argument n% is an integer expression that is the number of a function key, a direction key, or a user-defined key. The values of n% are as follows. ----------------------------------------------------------------------------- n% Value ---------------------------------------------------------------------------- 0 All keys listed in this table 1-10 F1-F10 11 Up Arrow key 12 Left Arrow key 13 Right Arrow key 14\~ Down Arrow key 15-25 User-defined keys 30-31 F11-F12 on 101-key keyboards The KEY( n%) ON statement enables trapping of function keys, direction keys, and user-defined keys. If key n% is pressed after a KEY( n%) ON statement, the routine specified in the ON KEY statement is executed. KEY( n% OFF disables trapping of key n%. No key trapping takes place until another KEY( n%) ON statement is executed. Events occurring while trapping is off are ignored. KEY( n%) STOP suspends trapping of key n%. No trapping takes place until a KEY( n%) ON statement is executed. Events occurring while trapping is suspended are remembered and processed when the next KEY( n%) ON statement is executed. However, remembered events are lost if KEY( n%) OFF is executed. When a key-event trap occurs (that is, the GOSUB is performed), an automatic KEY( n%) STOP is executed so that recursive traps cannot take place. The RETURN operation from the trapping routine automatically performs a KEY( n%) ON statement unless an explicit KEY( n%) OFF was performed inside the subroutine. For more information on event trapping, see Chapter 9, "Event Handling" in the Programmer's Guide. In addition to providing the preassigned key numbers 1-14 (plus 30 and 31 on the 101-key keyboard), BASIC enables you to create user-defined keys. You do this by assigning the numbers 15-25 to any of the remaining keys on the keyboard. Use the KEY statement (assignment) to create user-defined keys. You also can set a trap for "shifted" keys. A key is shifted when you press it simultaneously with one or more of the special keys Shift, Ctrl, or Alt after pressing NumLock or Caps Lock. Use the KEY statement (assignment) to define shifted keys before you trap them. The syntax for KEY (assignment) is. KEY n%, CHR$ ( keyboardflag) + CHR$ ( scancode) The argument n% is in the range 15-25 to indicate a user-defined key. The argument keyboardflag can be any combination of the following values. ----------------------------------------------------------------------------- Value Key ---------------------------------------------------------------------------- Value Key ---------------------------------------------------------------------------- 0 No keyboard flag 1, 2, or 3 Either Shift key 4 Ctrl 8 Alt 32 NumLock 64 Caps Lock 128 101-key keyboard extended keys You can add the values together to test for multiple shift states. A keyboardflag value of 12 would test for both Ctrl and Alt being pressed, for example. To define Shift, Ctrl, Alt, NumLock, or Caps Lock as a user-defined key (by itself, not in combination with another key), use a keyboard flag of 0. For example, to define Alt as a user-defined key, use the following statement. KEY CHR$(0) + CHR$(56) To define Alt + Alt as a user-defined key (the second Alt will be trapped when it is pressed only if the first Alt key is already being pressed), use the following statement. KEY CHR$(8) + CHR$(56) Because key trapping assumes the left and right Shift keys are the same, you can use 1, 2, or 3 to indicate a Shift key. The argument scancode is a number that identifies one of the 83 keys to trap, as shown in the following table. Keyboard Scan Codes ----------------------------------------------------------------------------- Key Code Key Code Key Code ---------------------------------------------------------------------------- Esc 1 Ctrl 29 Spacebar 57 ! or 1 2 A 30 Caps Lock 58 Key Code Key Code Key Code ---------------------------------------------------------------------------- ! or 1 2 A 30 Caps Lock 58 @ or 2 3 S 31 F1 59 # or 3 4 D 32 F2 60 $ or 4 5 F 33 F3 61 % or 5 6 G 34 F4 62 ^ or 6 7 H 35 F5 63 & or 7 8 J 36 F6 64 * or 8 9 K 37 F7 65 ( or 9 10 L 38 F8 66 ) or 0 11 . or ; 39 F9 67 _ or - 12 " or ' 40 F10 68 + or = 13 ~ or ` 41 NumLock 69 Backspace 14 Left Shift 42 Scroll Lock 70 Tab 15 | or \ 43 Home or 7 71 Q 16 Z 44 Up or 8 72 W 17 X 45 PgUp or 9 73 E 18 C 46 Gray - 74 R 19 V 47 Left or 4 75 T 20 B 48 Center or 5 76 Key Code Key Code Key Code ---------------------------------------------------------------------------- T 20 B 48 Center or 5 76 Y 21 N 49 Right or 6 77 U 22 M 50 Gray + 78 I 23 < or , 51 End or 1 79 O 24 > or . 52 Down or 2 80 P 25 ? or - 53 PgDn or 3 81 { or [ 26 Right Shift 54 Ins or 0 82 } or ] 27 PrtSc or * 55 Del or . 83 Enter 28 Alt 56 Note The scan codes in the preceding table are equivalent to the first column of the scan code table in Appendix A, "Keyboard Scan Codes and ASCII Character Codes." The codes in the other columns of the table in the appendix should not be used for key trapping. See Also KEY (Assignment), ON event Example The following example traps the Down Arrow key and Ctrl+s (Control key and lowercase "s"). To trap the combination of the Ctrl key and uppercase "s", trap Ctrl+Shift and I = 0 CLS' Clear screen. PRINT "Press Down Arrow key to end." KEY 15, CHR$(&H4) + CHR$(&H1F) KEY(15) ON ' Trap Ctrl+s. KEY(14) ON ' Trap Down Arrow key. ON KEY(15) GOSUB Keytrap ON KEY(14) GOSUB Endprog Idle. GOTO Idle' Endless loop. Keytrap. ' Counts the number of times Ctrl+s pressed. I = I + 1 RETURN Endprog. PRINT "CTRL+s trapped"; I; "times" END RETURN